<?php

namespace plugin\admin\app\model;

use plugin\admin\app\model\Base;

/**
 * @property integer $id (主键)
 * @property integer $type 类型
 * @property string $full 满多少金额
 * @property integer $count 已发放数量
 * @property string $reduce 减多少金额
 * @property string $deadline 截止时间
 */
class Coupon extends Base
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wa_coupons';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';
    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    const TYPE_FULL_REDUCE = 1;

    const TYPE_ANYWAY = 2;

    const TYPE_TEXT = [
        self::TYPE_FULL_REDUCE => '满减',
        self::TYPE_ANYWAY => '无门槛'
    ];

    public static function getTypeText($type)
    {
        return self::TYPE_TEXT[$type];
    }

    public static function getTotalCountText($totalCount)
    {
        return $totalCount == -1 ? '无限' : '有限';
    }

    public static function getRangeText(&$coupon, $categories)
    {
        if (empty($coupon['goods_category_ids'])) {
            $coupon['use_range_text'] = '所有品类可用';
            $coupon['goods_category_text'] = '所有品类';
        } else {
            $cids = explode(',', $coupon['goods_category_ids']);

            $coupon['goods_category_text'] = [];
            foreach ($categories as $category) {
                if (in_array($category['id'], $cids)) {
                    $coupon['goods_category_text'][] = $category['name'];
                }
            }

            $coupon['goods_category_text'] = join(',', $coupon['goods_category_text']);
            $coupon['use_range_text'] = '部分品类可用';
        }
    }
}
